//
//  ReadingWebViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ReadingWebViewController.h"
#import "readWebViewModel.h"

@interface ReadingWebViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) readWebViewModel *modelV;

@end

@implementation ReadingWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (1 == self.i) {
      self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:(UIBarButtonItemStylePlain) target:self action:@selector(returnVCAction)];
    } else {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:(UIBarButtonItemStylePlain) target:self action:@selector(returnAction)];
    }
    self.view.backgroundColor = [UIColor redColor];
    [self createWebView];
}

- (void)createWebView
{
    self.webView = [UIWebView new];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
        
    }];
    NSString *htmlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.code];
    self.webView.delegate = self;
    [self loadDetailDataWithURLStr:htmlStr];
}

- (void)loadDetailDataWithURLStr:(NSString *)urlStr
{
    NSString *url_string = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [xwzAFNetTool getNetWithURL:url_string body:nil headFile:nil responStyle:(XWZ_JSON) success:^(NSURLSessionDataTask *task, id responseObject) {
       
        NSDictionary *mulDic = [responseObject objectForKey:self.code];
        self.modelV = [[readWebViewModel alloc] init];
        [self.modelV setValuesForKeysWithDictionary:mulDic];
        
        [self loadMyWebView];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (void)loadMyWebView
{
    [self loadIMG];
    self.modelV.body = [NSString stringWithFormat:@"<h2>%@</h2>%@  %@\n%@\n", self.modelV.title, self.modelV.source, self.modelV.ptime, self.modelV.body];
    [self.webView loadHTMLString:self.modelV.body baseURL:nil];
}

- (void)loadIMG
{
    for (NSInteger i = 0; i < self.modelV.img.count; i++) {
        
        NSString *length = [[self.modelV.img objectAtIndex:i] objectForKey:@"pixel"];
        NSDictionary *dic = [self.modelV.img objectAtIndex:i];
#pragma 存放width height
        NSArray *arr = [length componentsSeparatedByString:@"*"];
        NSString *imgStr = [NSString stringWithFormat:@"<!--IMG#%ld-->", i];
        // 拼接
        CGFloat height = [arr[0] integerValue];
        CGFloat width = [arr[1] integerValue];
        width = self.view.frame.size.width - 20;
        height = width * 0.6;
        
        NSString *str = [NSString stringWithFormat:@"<img src='%@'  height='%f' width='%f' />\n",[dic objectForKey:@"src"], height, width];
        
        // 拼接内容
        self.modelV.body = [self.modelV.body stringByReplacingOccurrencesOfString:imgStr withString:str];
    }
}

- (void)returnAction
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showView" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)returnVCAction
{
    self.navigationController.navigationBarHidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
